package org.example.src.service.impl;

import org.example.src.dto.request.ApplicationRequest;
import org.example.src.dto.response.ApplicationResponse;
import org.example.src.entity.*;
import org.example.src.repository.*;
import org.example.src.service.ApplicationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private ApplicationRepository applicationRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private JobPostRepository jobPostRepository;

    @Autowired
    private CompanyRepository companyRepository;

    @Override
    public List<Application> findAllApplications() {
        return applicationRepository.findAll();
    }

    @Override
    public Application findApplicationById(Long id) {
        return applicationRepository.findById(id).orElse(null);
    }

    @Override
    public Application createApplication(ApplicationRequest request) {
        Student student = studentRepository.findById(request.getStudentId())
                .orElseThrow(() -> new RuntimeException("学生未找到"));

        JobPost jobPost = jobPostRepository.findById(request.getJobPostId())
                .orElseThrow(() -> new RuntimeException("职位未找到"));

        // 检查是否已经申请过该职位
        if (applicationRepository.findByStudentAndJobPost(request.getStudentId(), request.getJobPostId()).isPresent()) {
            throw new RuntimeException("您已经申请过该职位");
        }

        Application application = new Application();
        application.setStudent(student);
        application.setJobPost(jobPost);
        application.setCompany(jobPost.getCompany());
        application.setCoverLetter(request.getCoverLetter());
        application.setStatus(ApplicationStatus.PENDING);

        return applicationRepository.save(application);
    }

    @Override
    public Application updateApplicationStatus(Long id, ApplicationStatus status, String feedback) {
        Application application = applicationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("申请未找到"));

        application.setStatus(status);
        application.setFeedback(feedback);
        application.setProcessedDate(java.time.LocalDateTime.now());

        return applicationRepository.save(application);
    }

    @Override
    public void deleteApplication(Long id) {
        applicationRepository.deleteById(id);
    }

    @Override
    public List<Application> findApplicationsByStudentId(Long studentId) {
        return applicationRepository.findByStudentId(studentId);
    }

    @Override
    public List<Application> findApplicationsByJobPostId(Long jobPostId) {
        return applicationRepository.findByJobPostId(jobPostId);
    }

    @Override
    public List<Application> findApplicationsByCompanyId(Long companyId) {
        return applicationRepository.findByCompanyId(companyId);
    }

    @Override
    public List<ApplicationResponse> getApplicationsByStudentId(Long studentId) {
        List<Application> applications = applicationRepository.findByStudentId(studentId);
        return applications.stream().map(this::convertToResponse).collect(Collectors.toList());
    }

    private ApplicationResponse convertToResponse(Application application) {
        ApplicationResponse response = new ApplicationResponse();
        BeanUtils.copyProperties(application, response);
        response.setStatus(application.getStatus().name());

        if (application.getStudent() != null) {
            response.setStudentId(application.getStudent().getId());
            response.setStudentName(application.getStudent().getFullName());
        }

        if (application.getJobPost() != null) {
            response.setJobPostId(application.getJobPost().getId());
            response.setJobPostTitle(application.getJobPost().getTitle());
        }

        if (application.getCompany() != null) {
            response.setCompanyId(application.getCompany().getId());
            response.setCompanyName(application.getCompany().getCompanyName());
        }

        return response;
    }
}
